<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/resources/modular/plugin/taglibs.jsp" %>
<!doctype html>
<head content="-我的">
    <%@ include file="/resources/modular/head/inside_head.jsp" %>
    <style type="text/css">
        .pointpage_dv1, .pointpage_dv2,
        .pointpage_dv3, .pointpage_dv4,
        .my_childAccount_managedv, .my_childAccount_pointpage,
        .my_childAccount_addchildrendv {
            display: none;
        }

        .my_childAccount_pointpagedv_ul ul {
            display: none;
        }

        .my_childAccount_pointpagedv_ul > li > p, .my_childAccount_pointpagedv_rightul > li > p {
            text-indent: 20px;
            line-height: 40px;
            color: #666;
            border-bottom: 1px solid #eee;
        }

        .my_childAccount_addBtn, .my_childAccount_removeBtn {
            display: none;
            font-style: normal;
        }

        .my_childAccount_pointpagedv_left .my_childAccount_removeBtn {
            display: inline;
            color: #f00;
            font-size: 14px;
        }

        .my_childAccount_pointpagedv_right .my_childAccount_addBtn {
            display: inline;
            color: #38adff;
            font-size: 14px;
        }
    </style>
</head>
<body class="GC_Menu_7" name="GC_indvidual">

<%@ include file="/resources/modular/plugin/mainMenu.jsp" %>

<div class="page" name="GC_individual_childuserinfo">

    <%@ include file="/resources/modular/plugin/mainTopMenu.jsp" %>

    <%@ include file="/resources/modular/plugin/individualMeun.jsp" %>

    <div class="page_right">
        <%--  子账户管理 begin --%>
        <div class="my_childAccount_page" id="GC_childuser_manager">
            <p class="my_childAccount_pagep1">*系统初始密码默认为：123456<br>*修改密码，若不需要设置新密码，可直接恢复初始密码</p>
            <p class="my_childAccount_pagep2"><a href="showaddusergroupView">添加角色</a></p>
            <div class="my_childAccount_pagedv">
                <span>角色名称</span>
                <span>创建时间</span>
                <span>操作</span>
            </div>
            <div class="my_childAccount_pageul1">

                <c:forEach items="${usergroups}" var="usergroup" varStatus="status">
                    <div>
                        <span>${usergroup.name}</span>
                        <span class="my_childAccount_handlebtn" usergroupid="${usergroup.id}">
                        	<a class="my_childAccount_resetpsd" href="javascript:;">管理</a>
                            <a class="my_childAccount_impower" href="javascript:;">授权</a>
                            <a class="my_childAccount_addchild" href="javascript:;">添加账户</a>
                        </span>
                        <div class="my_childAccount_managedv" id="GC_child_usergroup_${usergroup.id}">
                                <%--  动态追加内容 --%>
                        </div>
                        <div class="my_childAccount_addchildrendv">
                            <p>子账户名称：<input id="GC_newchilduser_name_${usergroup.id}"
                                            class="my_childAccount_addchildrenname" type="text" placeholder="请输入子账户名称">
                            </p>
                            <p>子账户密码：<input id="GC_newchilduser_pass_${usergroup.id}"
                                            class="my_childAccount_addchildrenpsd" type="password"
                                            placeholder="请输入子账户密码"></p>
                            <span style="display:inline-block;margin-left:432px;">
                            	<a class="my_childAccount_addchildrendvbtn1" usergroupid="${usergroup.id}"
                                   href="javascript:;">确认</a>
                            	<a class="my_childAccount_addchildrendvbtn2" href="javascript:;">取消</a>
                            </span>
                        </div>
                        <!-- 授权管理 -->
                        <div class="my_childAccount_pointpage" style="background:#EEE;">
                            <div class="my_childAccount_pointpagedv">
                                <div class="my_childAccount_pointpagedv_left">

                                    <p class="my_childAccount_pointpagep2">已选授权名称</p>
                                    <ul class="my_childAccount_pointpagedv_ul"
                                        id="GC_MODULE_USEDMODULE_${usergroup.id}">

                                    </ul>

                                </div>
                                <!--可选择-->
                                <div class="my_childAccount_pointpagedv_right">

                                    <p class="my_childAccount_pointpagep2">可选授权名称</p>
                                    <ul class="my_childAccount_pointpagedv_ul"
                                        id="GC_MODULE_AVAILABLEMODULE_${usergroup.id}">

                                    </ul>

                                </div>
                                <!--可选择  -->
                            </div>
                            <p class="my_childAccount_pointpagep3">
                                <a class="my_childAccount_addchildbtn1" href="javascript:;"
                                   GC_USERGROUPID="${usergroup.id}">确认</a>
                                <a class="my_childAccount_addchildbtn2" href="javascript:;">取消</a></p>
                        </div>
                        <!-- 授权管理 end -->
                    </div>
                </c:forEach>


            </div>
        </div>
        <%--  子账户管理 end --%>

        <%--  子账户修改密码 begin --%>
        <div class="my_childAccount_resetpsd_dv" id="GC_changepassword" style="display: none;">
            <span>修改密码：</span>
            <input type="hidden" id="GC_changepassword_id">
            <input type="password" id="GC_changepassword_val" placeholder="请输入新密码">
            <a href="javascript:void(0);" id="GC_changepassword_butten">确认</a>
        </div>
        <%--  子账户修改密码 end --%>

    </div>


</div>
<!-- 删除角色 提示弹窗 -->
<div id="GC_childinfo_remove" class="pointpage_dv3">
    <div class="pointpage3">
        <p>是否删除子账号“<span id="GC_childinfo_remove_name"></span>”</p>
        <input type="hidden" id="GC_childinfo_remove_userid"/>
        <p style="line-height: 70px;text-align:center;"><a class="my_childAccount_deletebtn1" id="GC_childinfo_remove_button" href="javascript:;">确认</a> <a
                class="my_childAccount_deletebtn2" href="javascript:;">取消</a></p>
    </div>
</div>
<!-- 删除角色 提示弹窗 end -->

<!-- 提示弹窗 end -->
<div id="GC_childinfo_natice" class="pointpage_dv1" style="display: none;">
    <div class="pointpage1">
        <p id="GC_childinfo_natice_msg" class="pointpage_beginpsd"></p>
    </div>
</div>
<!--  提示弹窗 end -->


<!-- 权限修改成功 提示弹窗 -->
<div class="pointpage_dv4">
    <div class="pointpage1">
        <p class="pointpage_beginpsd">权限修改成功</p>
    </div>
</div>
<!-- 权限修改成功 提示弹窗 end -->

<%--  权限修改成功 提示弹窗 --%>
<div class="pointpage_dv4" style="display: none;">
    <div class="pointpage1">
        <p class="pointpage_beginpsd"></p>
        <p><a class="my_placemanage_confirmbtn1" href="javascript:;">确认</a></p>
    </div>
</div>
<%--  权限修改成功 提示弹窗 end --%>

</body>
</html>
<%@ include file="/resources/modular/foot/inside_footer.jsp" %>
<script type="text/javascript" src="${basePath}resources/js/v2/youyicloud-md5.js"></script>
<script type="text/javascript" src="${basePath}resources/js/v2/improve.js"></script>
<script type="text/javascript">

    var regName = /^\S{6,16}$/;
    var reg_childpsd = /^\S{6,16}$/;

    <%-- 角色授权 --%>
    function getrolemodules(usergroupid) {
        if (typeof(usergroupid) == "undefined" || usergroupid.length <= 0) {
            alert("非法操作");
        } else {
            $.ajax({
                url: '${basePath}v2/individual/getUserGroupModule',
                type: 'POST',
                data: {
                    "usergroupid": usergroupid
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $("#GC_MODULE_AVAILABLEMODULE_" + usergroupid).children().remove();
                        $.each(ret.availablemodule, function (index, module) {
                            var availablehtml = '<li GC_USERINFO_ROLE_MODULEID="' + module.id + '"><p class="my_childAccountId_' + (module.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module.id + '">' + module.text + '（可选择）</span><a href="javascript:;">' +
                                    '<var class="my_childAccount_addBtn">全部添加</var><var class="my_childAccount_removeBtn">全部删除</var></a></p><ul class="">';
                            $.each(module.children, function (i, module1) {
                                availablehtml = availablehtml + '<li GC_USERINFO_ROLE_MODULEID="' + module1.id + '"><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module1.id + '">' + module1.text + '</span>' +
                                        '<a href="javascript:;"><var class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p>' +
                                        '<ul class="">';

                                $.each(module1.children, function (j, module2) {
                                    availablehtml = availablehtml + '<li GC_USERINFO_ROLE_MODULEID="' + module2.id + '"><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '"><span class="my_childAccount_name" GC_MODULEID="' + module2.id + '">' + module2.text + '</span><a href="javascript:;"><var' +
                                            ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p><ul class="">';

                                    $.each(module2.children, function (l, module3) {
                                        availablehtml = availablehtml + '<li GC_USERINFO_ROLE_MODULEID="' + module3.id + '"><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '_' + module3.id + '"><span GC_MODULEID="' + module3.id + '">' + module3.text + '</span><a href="javascript:;"><var' +
                                                ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p></li>';
                                    });
                                    availablehtml = availablehtml + '</ul></li>';
                                });
                                availablehtml = availablehtml + '</ul></li>';
                            });
                            availablehtml = availablehtml + '</li></ul></li>';

                            $("#GC_MODULE_AVAILABLEMODULE_" + usergroupid).append(availablehtml);
                        });

                        $("#GC_MODULE_USEDMODULE_" + usergroupid).children().remove();
                        $.each(ret.usedmodule, function (index, module) {
                            var usedhtml = '<li><p class="my_childAccountId_' + (module.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module.id + '">' + module.text + '（可选择）</span><a href="javascript:;">' +
                                    '<var class="my_childAccount_addBtn">全部添加</var><var class="my_childAccount_removeBtn">全部删除</var></a></p><ul class="">';

                            $.each(module.children, function (i, module1) {
                                usedhtml = usedhtml + '<li><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '"><span class="my_childAccount_name" GC_MODULEID="' + module1.id + '">' + module1.text + '</span>' +
                                        '<a href="javascript:;"><var class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p>' +
                                        '<ul class="">';

                                $.each(module1.children, function (j, module2) {
                                    usedhtml = usedhtml + '<li><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '"><span class="my_childAccount_name" GC_MODULEID="' + module2.id + '">' + module2.text + '</span><a href="javascript:;"><var' +
                                            ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p><ul class="">';

                                    $.each(module2.children, function (l, module3) {
                                        usedhtml = usedhtml + '<li><p class="my_childAccountId_' + (module.id) + '_' + (module1.id) + '_' + module2.id + '_' + module3.id + '"><span GC_MODULEID="' + module3.id + '">' + module3.text + '</span><a href="javascript:;"><var' +
                                                ' class="my_childAccount_removeBtn">删除</var><var class="my_childAccount_addBtn">添加</var></a></p></li>';

                                    });
                                    usedhtml = usedhtml + '</ul></li>';
                                });
                                usedhtml = usedhtml + '</ul></li>';
                            });
                            usedhtml = usedhtml + '</li></ul></li>';
                            $("#GC_MODULE_USEDMODULE_" + usergroupid).append(usedhtml);

                        });


                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    }

    <%-- 添加子账户 --%>
    $(".my_childAccount_addchildrendvbtn1").click(function () {
        var usergroupid = $(this).attr("usergroupid");
        var username = $("#GC_newchilduser_name_" + usergroupid).val();
        var password = $("#GC_newchilduser_pass_" + usergroupid).val();

        if (!regName.test(username)) {
            alert("子账户登录名至少6位,最大16位字符");
        } else if (!reg_childpsd.test(password)) {
            alert("子账户密码至少6位,最大16位字符");
        } else {
            $.ajax({
                url: '${basePath}v2/individual/addchilduser',
                type: 'POST',
                data: {
                    "username": username,
                    "password": createPassword(password, password),
                    "role": usergroupid,
                    "state": 1
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $("#GC_childinfo_natice_msg").text("子账户添加成功");
                        $("#GC_childinfo_natice").show();
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }

    });

    <%-- 修改密码 --%>
    $("#GC_changepassword_butten").click(function () {
        var userid = $("#GC_changepassword_id").val();
        var password = $("#GC_changepassword_val").val();
        if (typeof(userid) == "undefined" || userid.length <= 0) {
            alert("操作异常");
        } else if (typeof(password) == "undefined" || password.length <= 0) {
            alert("密码不能为空");
        } else {
            $.ajax({
                url: '${basePath}v2/individual/changechilduserpass',
                type: 'POST',
                data: {
                    "userid": userid,
                    "password": createPassword(password, password)
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $(".my_childAccount_impower").css("background", "#FFF");
                        $(".my_placemanage_impower").css("background", "#FFF");
                        $(".pointpage_beginpsd").text("密码修改成功");
                        $(".pointpage_dv4").stop().show();
                        $(".my_placemanage_confirmbtn1").on("click", function () {
                            $(".pointpage_dv4").stop().hide();
                            $("#GC_changepassword").hide();
                            $("#GC_childuser_manager").show();
                        });
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    });

    <%-- 获取用户组下所有子帐号 --%>
    $(".my_childAccount_resetpsd").click(function () {
        var usergroupid = $(this).parent().attr("usergroupid");
        $.ajax({
            url: '${basePath}v2/individual/getchilduser',
            type: 'POST',
            data: {
                "usergroupid": usergroupid
            },
            error: function (response) {
                $errorMsg(response);
            },
            success: function (ret) {
                if (ret.result) {
                    $("#GC_child_usergroup_" + usergroupid).children().remove();
                    $.each(ret.userinfos, function (index, obj) {
                        $childhtml = '<div id="GC_child_usergroup_userinfo_' + obj.userid + '" class="my_childAccount_resetpsddv"><span id="GC_CHILDUSER_INFO_'+obj.userid+'">' + obj.username + '</span><time>' + obj.regTime + '</time><a class="my_childAccount_deletebutton" href="javascript: void(0);" ' +
                                'onclick="showrovemochilduser('+ obj.userid + ');">删除</a><a href="javascript:showchangepassword(' + obj.userid + ');">修改密码</a><a class="my_childAccount_beginpsd" ' +
                                'href="javascript:recoverPassowrd(' + obj.userid + ');">恢复初始密码</a></div>';
                        $("#GC_child_usergroup_" + usergroupid).append($childhtml);
                    });
                } else {
                    alert(ret.info);
                }
            }
        });
    });

    <%-- 恢复密码 --%>
    function recoverPassowrd(val) {
        if (typeof(val) == "undefined" || val.length <= 0) {
            alert("操作异常");
            return;
        } else {
            if (confirm("是否恢复子账户初始密码?")) {
                $.ajax({
                    url: '${basePath}v2/individual/recoverpassword',
                    type: 'POST',
                    data: {
                        "userid": val
                    },
                    error: function (response) {
                        $errorMsg(response);
                    },
                    success: function (ret) {
                        if (ret.result) {
                            $("#GC_childinfo_natice_msg").text("初始密码已恢复");
                            $("#GC_childinfo_natice").show();
                        } else {
                            alert(ret.info);
                        }
                    }
                });
            }
        }
    }

    <%-- 删除子账户 --%>
    $("#GC_childinfo_remove_button").click(function () {
        var val = $("#GC_childinfo_remove_userid").val();
        if (typeof(val) == "undefined" || val.length <= 0) {
            alert("操作异常");
            return;
        } else {
            $("#GC_childinfo_remove").hide();
            $.ajax({
                url: '${basePath}v2/individual/removechilduser',
                type: 'POST',
                data: {
                    "userid": val
                },
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        $("#GC_child_usergroup_userinfo_" + val).remove();
                        alert("子账户已删除", "cur");
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    })

    $(".my_childAccount_addchildbtn1").click(function () {
        var modulesArray = new Array();
        var roleid = $(this).attr("GC_USERGROUPID");
        $("#GC_MODULE_USEDMODULE_" + roleid + " span").each(function (index, obj) {
            modulesArray[index] = $(obj).attr("GC_MODULEID");
        })

        setupmodule(roleid, modulesArray);
    });


    <%-- 设置角色权限 --%>
    function setupmodule(usergroupid, modules) {
        if (typeof(usergroupid) == "undefined" || usergroupid.length <= 0) {
            alert("操作异常");
            return;
        } else if (typeof(modules) == "undefined" || modules.length <= 0) {
            alert("操作异常");
            return;
        } else {
            $.ajax({
                url: '${basePath}v2/individual/setuprolemodules',
                type: 'POST',
                data: {
                    "roleid": usergroupid,
                    "modules": modules
                },
                traditional: true,
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {
                        alert("角色权限分配成功", "cur");
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    }


    <%-- 显示修改密码框，隐藏子账户管理界面 --%>
    function showrovemochilduser(userid) {
        var username = $("#GC_CHILDUSER_INFO_"+userid).text();
        if (typeof(username) == "undefined" || username.length <= 0) {
            alert("操作异常");
            return;
        } else if (typeof(userid) == "undefined" || userid.length <= 0) {
            alert("操作异常");
            return;
        } else {
            $("#GC_childinfo_remove_name").text(username);
            $("#GC_childinfo_remove_userid").val(userid);
            $("#GC_childinfo_remove").show();
        }
    }

    <%-- 显示修改密码框，隐藏子账户管理界面 --%>
    function showchangepassword(val) {
        if (typeof(val) == "undefined" || val.length <= 0) {
            alert("操作异常");
            return;
        } else {
            $("#GC_changepassword_id").val(val);
            $("#GC_changepassword").show();
            $("#GC_childuser_manager").hide();
        }
    }

</script>
